package com.zmn.oms.admin.controller.conf.orderduty;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.model.entity.conf.orderduty.OrderDuty;
import com.zmn.oms.model.entity.conf.orderduty.OrderDutyQuery;
import com.zmn.oms.services.interfaces.conf.orderduty.OrderDutyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 类描述：预约设置基础修改
 *
 * @author liuying
 * @date 2022/03/09 18:25
 */
@Slf4j
@RestController
@RequestMapping("/conf/orderduty/")
public class OrderDutyModifyController extends DutyBaseController {

    @Resource
    private OrderDutyService orderDutyService;

    /**
     * 批量保存
     */
    @RequestMapping("addEditList")
    //@RequiresPermissions("/conf/orderduty/addEditList.action")
    public ResponseDTO addEditList(HttpServletRequest request, @RequestBody List<OrderDuty> settings) {

        Date now = DateUtil.getNow();
        List<OrderDuty> insetBatchList = new ArrayList<>();
        List<OrderDuty> updateBatchList = new ArrayList<>();
        for (OrderDuty setting : settings) {
            int id = setting.getId();
            if (id == 0) {
                OrderDutyQuery query = OrderDutyQuery.builder()
                        .appointmentOrChange(setting.getAppointmentOrChange())
                        .plat(setting.getPlat())
                        .type(setting.getType())
                        .build();
                List<OrderDuty> orderDutyList = orderDutyService.listByQuery(query);
                if (orderDutyList == null || orderDutyList.size() == 0) {
                    setting.setCreater(getMcStaffRealName());
                    setting.setCreateTime(now);
                    setting.setUpdateTime(now);
                    insetBatchList.add(setting);
                } else if (orderDutyList.size() == 1) {
                    OrderDuty orderDuty = orderDutyList.get(0);
                    setting.setId(orderDuty.getId());
                }
            }
            if (id != 0) {
                setting.setUpdater(getMcStaffRealName());
                setting.setUpdateTime(now);
                updateBatchList.add(setting);
            }
        }
        if (insetBatchList.size() > 0) {
            orderDutyService.insertBatch(insetBatchList);
        }
        if (updateBatchList.size() > 0) {
            orderDutyService.updateBatchByKey(updateBatchList);
        }
        return ResponseDTO.success(true, "操作成功！");

    }

}
